package tech.aistar.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import tech.aistar.entity.Post;
import tech.aistar.entity.UserInfo;

import java.util.List;

/**
 * Created by Administrator on 2019/10/8.
 */
public interface PostMapper {
    //查询所有帖子
    @Select("select * from post")
    List<Post> findAllPost();

    //根据帖子id查询帖子所有内容
    @Select("select * from post where post_id = #{postId}")
    Post findPostByPostId(Integer postId);

    //查询主题分类对应帖子
    @Select("select * from post where theme_id = #{theme_id} and class_id = #{class_id}")
    List<Post> findPostByThemeAndClass(@Param("theme_id") Integer themeId, @Param("class_id") Integer classId);

    //查询主题对应帖子
    @Select("select * from post where theme_id = #{theme_id}")
    List<Post> findPostByTheme(Integer themeId);

    //主题对应帖子个数
    @Select("select count(*) from post where theme_id = #{theme_id}")
    Long findPostNumByTheme(Integer themeId);

    //帖子点赞数

    //根据用户id查询所有该用户帖子
    @Select("select * from post where user_id = #{user_id}")
    List<Post> findAllPostByUserId(Integer userId);

    //增加帖子
    @Insert("insert into post(theme_id,class_id,post_content,user_id,status,title,ptime) values(#{themeId},#{classId},#{postContent},#{userId},#{status},#{title},#{ptime})")
    void savePost(Post post);

    //删除帖子
    @Update("update post set status = 1 where post_id = #{value}")
    void delPost(Integer postId);

    //查询用户的名字与头像
    @Select("select u_name,picfilename from user_info where id in (select user_id from post where post_id = #{postId})")
    UserInfo findNameImg(Integer postId);

}
